package com.jianyun.jyzs.fragment;

import android.annotation.SuppressLint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.SelectEProjectActivity;
import com.jianyun.jyzs.adapter.SelectP2Adapter;
import com.jianyun.jyzs.base.MVPBaseFragment;
import com.jianyun.jyzs.bean.EngProjectLocResult;
import com.jianyun.jyzs.bean.EngineerProjectResult;
import com.jianyun.jyzs.common.ITextWatchListener;
import com.jianyun.jyzs.dao.EngLocationDao;
import com.jianyun.jyzs.presenter.P2FragementPresenter;
import com.jianyun.jyzs.view.iview.IP2FragmentView;
import com.jrmf360.rylib.common.util.ToastUtil;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.Unbinder;

/**
 * Created by yangshijun on 2018/5/31.
 */

@SuppressLint("ValidFragment")
public class P2FragmentAllFragment extends MVPBaseFragment<IP2FragmentView, P2FragementPresenter> implements IP2FragmentView {
    @BindView(R.id.search_edit)
    EditText searchEdit;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    Unbinder unbinder;

    private String p2id;
    private SelectP2Adapter adapter;
    private List<EngProjectLocResult.EngTypeLocBean> engineer;
    private EngLocationDao dao;
    private EngineerProjectResult.EngProject project;


    @Override
    protected void initView() {

    }


    public P2FragmentAllFragment(EngineerProjectResult.EngProject project) {
        this.p2id = project.getAutoId() + "";
        this.project = project;
    }

    protected void initData() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        dao = new EngLocationDao(getActivity());
        adapter = new SelectP2Adapter((SelectEProjectActivity) getActivity(), null, project);
        recyclerView.setAdapter(adapter);
        engineer = dao.getEngineer(p2id);
        if (engineer.size() == 0) {
            HashMap<String, String> map = new HashMap<>();
            map.put("method", "GetProJectPostionList");
            map.put("ProJectId", p2id);
            map.put("enterpriseCode", ((SelectEProjectActivity) getActivity()).enterpriseCode);
            map.put("appCode", "jyzs");
            map.put("appType", "Android");
            map.put("appVersion", "1.0.2");
            getPresenter().getP2DataToService(dao, map);
        } else {
            adapter.setList(engineer);
        }
        searchEdit.addTextChangedListener(new ITextWatchListener() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = s.toString();
                setP2View(str);
            }
        });
    }

    @Override
    protected int initContentLayout() {
        return R.layout.fragment_p2_all;
    }

    private void setP2View(String str) {
        if (TextUtils.isEmpty(str)) {
            engineer = dao.getEngineer(p2id);
        } else {
            engineer = dao.getEngineerSearch(str, p2id);
        }
        adapter.setList(engineer);
    }

    @Override
    public P2FragementPresenter createPresenter() {
        return new P2FragementPresenter();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onSuccess() {
        engineer = dao.getEngineer(p2id);
        adapter.setList(engineer);
    }

    @Override
    public void onFailed(String msg) {
        ToastUtil.showToast(getContext(), msg);
    }
}
